package com.example.spider.provider.why10w.bmcx;

import cn.hutool.core.util.ReUtil;
import com.example.spider.provider.why10w.AbstractWhy10wListResultHandler;
import com.example.spider.provider.why10w.dto.Why10wDetailTask;
import com.example.spider.provider.why10w.dto.Why10wListTask;
import com.example.spider.task.SpiderTaskQueue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.time.Duration;
import java.util.List;

/**
 * 处理列表查询结果
 *
 * @author lym
 */
@Slf4j
//@Component
public class BmcxListResultHandler extends AbstractWhy10wListResultHandler {

    private static final String DOMAIN_URL = "";

    @Override
    protected String buildPageTaskUrl(String firstCategory, String secondCategory, int pageNo) {
        return "https://swgwsm.bmcx.com/c0ozrtx8_" + pageNo + "__swgwsmlist/";
    }

    /**
     * 1-7 ok
     */
    @Override
    public void startSpiders() {
        for (int i = 1; i <= 1; i++) {
            SpiderTaskQueue.putTask(createPageTask(null, null, i));
            //        // 两个系列每个系列单独限流
            //        // 动物 1-10  11 之后是重复的历史系列 历史 20
            //        list 没有限流，直接本机请求即可，然后详情页用一般的代理不断重试就好了
        }
    }

    /**
     * 分析列表结果，创建下载详情任务
     */
    @Override
    public void doHandle(Why10wListTask task, String html) {

        List<String> contentMatchResult = ReUtil.findAllGroup1("<ul class=\"list\">(.*?</a></li></ul>)", html);
        if (CollectionUtils.isEmpty(contentMatchResult)) {
            log.warn("illegal BmcxList result " + task.getUrl());
            return;
        }
        String content = contentMatchResult.get(0);

        String regex = "<li><a href=\"(.*?)\" target=\"_blank\">(.*?)</a></li>";
        List<String> detailUrlList = ReUtil.findAll(regex, content, 1);
        List<String> titleList = ReUtil.findAll(regex, content, 2);

        for (int i = 0; i < detailUrlList.size(); i++) {
            String title = titleList.get(i);
            title = title.replace(" ", "").replace("\n", "").trim();
            String url = detailUrlList.get(i);
            String id = url.replace("/", "");
            Why10wDetailTask detailTask = createDetailTask(task, id, title, "https://swgwsm.bmcx.com" + url);

            // 创建爬取详情的任务
            SpiderTaskQueue.putTask(detailTask);
        }
        repository.saveHtml(task, html);
    }

    @Override
    protected Duration limitDetailDuration() {
        return Duration.ofMillis(3000);
    }
}
